package com.person.jc.service.car;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.Utils;

public class BdCarServiceInfoService extends Service {
	
	public static final BdCarServiceInfoService me = new BdCarServiceInfoService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql= new StringBuffer("select *,date_format(service_date, '%Y-%m-%d %H:%i:%s') service_date_ from bs_car_service_info where oper_user = ? ");
		params.add(map.get("operUser"));
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("carNo")))){
			sql.append(" and car_no = ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("serviceId")))){
			sql.append(" and service_id = ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("serviceUser")))){
			sql.append(" and service_user like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("startDate")))){
			sql.append(" and date_format(service_date, '%Y-%m-%d') >= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("endtDate")))){
			sql.append(" and date_format(service_date, '%Y-%m-%d') <= ?");
			params.add(temp);
		}
		sql.append(" order by service_date desc");
		return Db.find(sql.toString(),Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		Record record = new Record();
		record.set("car_no", map.get("carNo"));
		record.set("service_mileage", map.get("serviceMileage"));
		record.set("service_unit", map.get("serviceUnit"));
		record.set("service_content", map.get("serviceContent"));
		record.set("service_date", map.get("serviceDate"));
		record.set("service_user", map.get("serviceUser"));
		record.set("remark", map.get("remark"));
		record.set("oper_user", map.get("operUser"));
		record.set("create_time", DateUtils.getDateTime());
		Object nextMileage = Utils.isNotEmpty(String.valueOf(map.get("nextMileage")))?map.get("nextMileage"):null;
		record.set("next_mileage", nextMileage);
		Db.save("bs_car_service_info", record);
		super.setCRUD("车辆保养管理", "C", "新增车辆保养信息："+map.get("carNo"));
	}
	
	public void update(Map<String,Object> map){
		Record record = new Record();
		record.set("service_id", map.get("serviceId"));
		record.set("car_no", map.get("carNo"));
		record.set("service_mileage", map.get("serviceMileage"));
		record.set("service_unit", map.get("serviceUnit"));
		record.set("service_content", map.get("serviceContent"));
		record.set("service_date", map.get("serviceDate"));
		record.set("service_user", map.get("serviceUser"));
		record.set("remark", map.get("remark"));
		Object nextMileage = Utils.isNotEmpty(String.valueOf(map.get("nextMileage")))?map.get("nextMileage"):null;
		record.set("next_mileage", nextMileage);
		Db.update("bs_car_service_info", "service_id",record);
		super.setCRUD("车辆保养管理", "U", "修改车辆保养信息，编号："+map.get("serviceId"));
	}
	
	
	public void delete(String serviceId){
		Db.deleteById("bs_car_service_info", "service_id", serviceId);
		super.setCRUD("车辆保养管理", "D", "删除车辆保养信息："+serviceId);
	}
}
